﻿using IOA.MES.Business;
using IOA.MES.DataAccess.VModel;
using IOA.MES.EsbApi.App_Start;
using IOA.Utils;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Web;

namespace IOA.MES.EsbApi
{
    /// <summary>
    /// 基础环境
    /// </summary>
    public class BaseEnv
    {
        /// <summary>
        /// 用户令牌
        /// </summary>
        public static string Token
        {
            get
            {
                var key = HttpContext.Current.Request.Headers.AllKeys.FirstOrDefault(_ => string.Equals(_, "Token", StringComparison.CurrentCultureIgnoreCase));
                if (!string.IsNullOrWhiteSpace(key))
                {
                    return HttpContext.Current.Request.Headers[key];
                }

                return null;
            }
        }

        /// <summary>
        /// 车间关联表
        /// </summary>
        public static List<KeyValuePair<string,string>> WorkShops
        {
            get
            {
                return null;
                //var key = ConfigurationManager.AppSettings["Workshops"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(_=>new KeyValuePair<string,string>)
                //if (!string.IsNullOrWhiteSpace(key))
                //{
                //    return HttpContext.Current.Request.Headers[key];
                //}

                //return null;
            }
        }

        /// <summary>
        /// 仓库代码
        /// </summary>
        public static string WarehouseCode
        {
            get
            {
                var key = HttpContext.Current.Request.Headers.AllKeys.FirstOrDefault(_ => string.Equals(_, "WarehouseCode", StringComparison.CurrentCultureIgnoreCase));
                if (!string.IsNullOrWhiteSpace(key))
                {
                    return HttpContext.Current.Request.Headers[key];
                }

                return null;
            }
        }

        /// <summary>
        /// 用户信息
        /// </summary>
        public static VUser User
        {
            get
            {
                return new UserMgr().GetUserByToken(Token);
            }
        }

        /// <summary>
        /// 仓库信息
        /// </summary>
        public static VWarehouse Warehouse
        {
            get
            {
                return new WarehouseMgr().GetWarehouse(WarehouseCode);
            }
        }

        /// <summary>
        /// 客户端IP
        /// </summary>
        public static string ClientIP
        {
            get
            {
                string ipv4 = String.Empty,
                    clientIp = HttpContext.Current.Request.ServerVariables["HTTP_VIA"] == null ? HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] : HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                foreach (var ip in Dns.GetHostAddresses(clientIp))
                {
                    if (ip.AddressFamily.ToString() == "InterNetwork")
                    {
                        ipv4 = ip.ToString();
                        break;
                    }
                }
                if (ipv4 != string.Empty)
                {
                    return ipv4;
                }
                foreach (var ip in Dns.GetHostEntry(clientIp).AddressList)
                {
                    if (ip.AddressFamily.ToString() == "InterNetwork")
                    {
                        ipv4 = ip.ToString();
                        break;
                    }
                }

                return ipv4;
            }
        }

        /// <summary>
        /// 客户端类型
        /// </summary>
        public static string ClientType
        {
            get
            {
                try
                {
                    if (HttpContext.Current.Request.UserAgent.ToLower().Contains("ios"))
                    {
                        return "iOS";
                    }
                    else if (HttpContext.Current.Request.UserAgent.ToLower().Contains("android"))
                    {
                        return "Android";
                    }
                    else
                    {
                        return "Web";
                    }
                }
                catch (Exception ex)
                {
                    return string.Empty;
                }
            }
        }

        /// <summary>
        /// 错误代码
        /// </summary>
        public static List<LangPackage> LangPackages { get; set; }
    }
}
